package com.adon.ar.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.Path.Direction;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.PathShape;
import android.os.Handler;
import android.os.Message;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class StoreView extends View {
	
	//-------------------------------------------------------------------------//
	private Camera camera;
	private Matrix mMatrix = new Matrix();
	private int mLastMotionX, mLastMotionY;
	public float [] matrixValues = new float[9];
	// 圖片旋轉的中心位置
	private int centerX, centerY;
	// 圖片轉動的距離
	private int deltaX, deltaY;
	// 圖片長、寬
	private int width, height;
	// 螢幕長、寬
	private int display_width, display_height;
	Display display;	
	//-------------------------------------------------------------------------//
	public enum StoreType { TYPE_GROUP_BUYING, TYPE_1288 }
	private StoreType mStoreType = StoreType.TYPE_GROUP_BUYING;
	
	private Bitmap mBackground			= null;
	private Bitmap mIcon				= null;
	
	private ShapeDrawable mRoundOut		= null;
	private ShapeDrawable mRoundIn		= null;

	private float mScale 		= 1.0f;	// scale the whole picture...
	private int mSize_Out_W 	= 360; // px
	private int mSize_Out_H 	= 160; // px
	private int mPivot_W 		= 40;
	private int mPivot_H 		= 24;
	private int mOffset		= 8;
	private int StrokeWidth   	= 8;
	private int mIcon_W		= 50;
	private int mIcon_H		= 50;
	
	private final static Paint 	mPaint 		= new Paint(Paint.ANTI_ALIAS_FLAG);
	private final static Paint 	mIconPaint 	= new Paint(Paint.ANTI_ALIAS_FLAG);
	private String		mName;
	private int    	mDistance;
//	//
	private Path path1 = null;
	private Path path2 = null;
//	//
	private final static TextPaint paintDistance 	= new TextPaint(Paint.ANTI_ALIAS_FLAG);
	private final static TextPaint paintName 		= new TextPaint(Paint.ANTI_ALIAS_FLAG);
	//-------------------------------------------------------------------------//
	public Handler mHandler = new Handler() {
		public void dispatchMessage(Message msg) {
			//
			createBubbleBitmap();
			//
			invalidate();
		}
	};

	public StoreView(Context context, String name, int distance, Bitmap icon, StoreType type) {
		super(context);
		// TODO Auto-generated constructor stub
		camera = new Camera();
		width 	= mSize_Out_W;
		height 	= mSize_Out_H;
		
		// 分別為圖片x軸及y軸的中點 >>除以2
		centerX = width >> 1;
		centerY = height >> 1;

		// setting screen size
		display = ((WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		display_height 	= display.getHeight();
		display_width 	= display.getWidth();
		//
		mName 		= name;
		mDistance 	= distance;
		mStoreType	= type;
		mIcon		= icon;
		//
		mScale = (float)display_width / (3 * 360);
		createPath();
		createBubbleBitmap();
	}
	
	public void resetDistance(int distance){
		mDistance 	= distance;
		mHandler.sendEmptyMessage(0);
	}

	private void createPath(){
		//--------------------------------------------------------------------------------------------------//
		path2 = new Path();
		RectF leftArc 	= new RectF();
		leftArc.left 	= 0*mScale;
		leftArc.top 	= 0*mScale;
		leftArc.right 	= (mSize_Out_H - mPivot_H)*mScale;
		leftArc.bottom 	= (mSize_Out_H - mPivot_H)*mScale;
		//path2.addArc(leftArc, 90, 180);
		path2.arcTo(leftArc, 90, 180);
		
		
		Path tmpPath2 = new Path();
		tmpPath2.moveTo(0, 0);
		tmpPath2.lineTo(((mSize_Out_W-(mSize_Out_H - mPivot_H)-mPivot_W)/2)*mScale, 0);
		path2.addPath(tmpPath2, ((mSize_Out_H - mPivot_H)/2)*mScale, (mSize_Out_H - mPivot_H)*mScale);
				
		Path pivot = new Path();
		pivot.moveTo(0, 0);
		pivot.lineTo((mPivot_W/2)*mScale, mPivot_H*mScale);
		pivot.lineTo(mPivot_W*mScale, 0);
		path2.addPath(pivot, ((mSize_Out_W - mPivot_W)/2)*mScale, (mSize_Out_H - mPivot_H)*mScale);	
		
		path2.addPath(tmpPath2, ((mSize_Out_W + mPivot_W)/2)*mScale, (mSize_Out_H - mPivot_H)*mScale);
		
		RectF rightArc 	= new RectF();
		rightArc.left   = (mSize_Out_W - ((mSize_Out_H - mPivot_H)))*mScale;
		rightArc.top    = 0*mScale;
		rightArc.right  = mSize_Out_W*mScale;
		rightArc.bottom = (mSize_Out_H - mPivot_H)*mScale;
		//path2.addArc(rightArc, 90, -180);
		path2.arcTo(rightArc, 90, -180);
		
		Path tmpPath1 = new Path();
		tmpPath1.moveTo((mSize_Out_W-(mSize_Out_H - mPivot_H))*mScale, 0*mScale);
		tmpPath1.lineTo(0, 0);
		path2.addPath(tmpPath1, ((mSize_Out_H - mPivot_H)/2)*mScale, 0);

		path2.close();
		//--------------------------------------------------------------------------------------------------//
		path1 = new Path();
		//path1.addArc(leftArc, 90, 180);
		path1.arcTo(leftArc, 90, 180);
		
		RectF midRect = new RectF();
		midRect.left	= ((mSize_Out_H - mPivot_H)/2)*mScale;
		midRect.top		= 0*mScale;
		midRect.right	= (mSize_Out_W - ((mSize_Out_H - mPivot_H)/2))*mScale;
		midRect.bottom  = (mSize_Out_H - mPivot_H)*mScale;
		path1.addRect(midRect, Direction.CCW);
		
		path1.addPath(pivot, ((mSize_Out_W - mPivot_W)/2)*mScale, (mSize_Out_H - mPivot_H)*mScale);
		
		//path1.addArc(rightArc, 90, -180);
		path1.arcTo(rightArc, 90, -180);
		
		path1.close();
	}
	
	private void clearObject(boolean clearIcon){
		if (mBackground != null){
			mBackground.recycle();
			mBackground = null;
		}
		if (mIcon != null && clearIcon == true){
			mIcon.recycle();
			mIcon = null;
		}
		if (mRoundOut != null){
			mRoundOut.setCallback(null);
			mRoundOut = null;
		}
		if (mRoundIn != null){
			mRoundIn.setCallback(null);
			mRoundIn = null;
		}
	}	
	
	private void createBubbleBitmap(){		
		//
		clearObject(false);
		//
		mBackground = Bitmap.createBitmap((int)(mSize_Out_W*mScale), (int)(mSize_Out_H*mScale), Bitmap.Config.ARGB_8888);
		mBackground.eraseColor(Color.TRANSPARENT);

		mRoundOut = new ShapeDrawable(new PathShape(path1, mSize_Out_W*mScale, mSize_Out_H*mScale));
		mRoundOut.setBounds(0, 0, (int)(mSize_Out_W*mScale), (int)(mSize_Out_H*mScale));
		mRoundOut.getPaint().setColor(0xFFFFFF);
		mRoundOut.getPaint().setAlpha(180);
		mRoundOut.getPaint().setStrokeJoin(Paint.Join.ROUND);
		mRoundOut.getPaint().setStrokeCap(Paint.Cap.ROUND);
		mRoundOut.getPaint().setAntiAlias(true);
		
		mRoundIn = new ShapeDrawable(new PathShape(path2, mSize_Out_W*mScale, mSize_Out_H*mScale));
		mRoundIn.setBounds((int)(mOffset*mScale), (int)(mOffset*mScale), (int)((mSize_Out_W-mOffset)*mScale), (int)((mSize_Out_H-mOffset*1.5f)*mScale));
		mRoundIn.getPaint().setColor(mStoreType==StoreType.TYPE_GROUP_BUYING ? 0xFF7E00 : 0x16B800 );
		mRoundIn.getPaint().setAlpha(180);
		mRoundIn.getPaint().setAntiAlias(true);
		mRoundIn.getPaint().setStyle(Paint.Style.STROKE);
		mRoundIn.getPaint().setStrokeJoin(Paint.Join.ROUND);
		mRoundIn.getPaint().setStrokeCap(Paint.Cap.ROUND);
		mRoundIn.getPaint().setStrokeWidth(StrokeWidth*mScale);

		Canvas canvas = new Canvas(mBackground);
		mRoundOut.draw(canvas);
		mRoundIn.draw(canvas);

		// for distance			
		paintDistance.setColor(Color.rgb(78, 78, 78));
		paintDistance.setTextSize(display_width / 26);
		paintDistance.setAntiAlias(true);
		paintDistance.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
		canvas.drawText(mDistance + "m", 126*mScale, 110*mScale, paintDistance);
		// for name			
		paintName.setColor(Color.BLACK);
		paintName.setTextSize(display_width / 25);
		paintName.setAntiAlias(true);
		paintName.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));			
		String out = TextUtils.ellipsize(mName, paintName, mSize_Out_W*mScale*0.7f, TextUtils.TruncateAt.END).toString();
		canvas.drawText(out, 90*mScale, 58*mScale, paintName);
		
		if (mIcon != null){
			Rect IconSrc = new Rect();
			IconSrc.left 	= 0;
			IconSrc.top 	= 0;
			IconSrc.right 	= mIcon.getWidth();
			IconSrc.bottom 	= mIcon.getHeight();
			RectF IconDst = new RectF();
			IconDst.left 	= 34*mScale;
			IconDst.top 	= 20*mScale;
			IconDst.right 	= IconDst.left + mIcon_W*mScale;
			IconDst.bottom 	= IconDst.top + mIcon_H*mScale;
			mIconPaint.setAlpha(200);
			canvas.drawBitmap(mIcon, IconSrc, IconDst, mIconPaint);	
		}

	}
	
	public int getTextureHeight() {
		return height;
	}

	public int getTextureWidth() {
		return width;
	}
	
	void translate(int a, int b, int c) {
		camera.save();
		camera.translate(a, b, c);
		camera.getMatrix(mMatrix);
		camera.restore();
		camera.save();

		// postInvalidate();
	}

	void rotateX(float a) {

		camera.save();
		camera.rotateZ(a);
		camera.getMatrix(mMatrix);
		camera.restore();
		camera.save();

		// 以圖片中心做旋轉
		mMatrix.preTranslate(-centerX, -centerY);
		mMatrix.postTranslate(centerX, centerY);
		camera.save();

		// postInvalidate();
	}

	float[] q = new float[9];

	private int rotateZ_Y = 0;
	
	public int getRotateZ_Y(){
		return rotateZ_Y;
	}
	
	void rotateZ(float a, int b, int c, int d) {
		
		rotateZ_Y = c;
		camera.save();
		camera.rotateZ(a);
		camera.translate(b, c, d);
		camera.getMatrix(mMatrix);
		camera.restore();
//		mMatrix.postScale(2, 2);
		// 以圖片中心做旋轉
		mMatrix.preTranslate(-centerX, -centerY);
		mMatrix.postTranslate(centerX, centerY);
		camera.save();

		// postInvalidate();
	}

	void rotate(int degreeX, int degreeY) {
		deltaX += degreeX;
		deltaY += degreeY;

		camera.save();
		camera.rotateY(deltaX);
		camera.rotateX(-deltaY);
		camera.translate(0, 0, -centerX);
		camera.getMatrix(mMatrix);
		camera.restore();

		// 以圖片中心做旋轉
		mMatrix.preTranslate(-centerX, -centerY);
		mMatrix.postTranslate(centerX, centerY);
		camera.save();

		// postInvalidate();
	}
	
	@Override
	public void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		{
			canvas.drawBitmap(mBackground, mMatrix, mPaint);
			mMatrix.getValues(matrixValues);
//			Log.d("ken", "mBitmapStatus:" + mBitmapStatus);
		}
	}	
	
	@Override
	protected void onDetachedFromWindow() {
		// TODO Auto-generated method stub
		clearObject(true);
		super.onDetachedFromWindow();
	}	
}
